package com.redpack.redpost.struts.form;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.axis.utils.StringUtils;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import paquete.codiceCode;

import com.redpack.redpost.util.dao.UserDAO;
import com.redpack.redpost.util.to.UserLogin;
import com.redpack.redpost.util.to.UserTo;
import com.redpack.redpost.util.to.validaciones;

public class dUsrForm extends ActionForm {
	
	private static final long serialVersionUID = 1L ;
	
	private String ntt ;
	private String tt ;
	private String nt ;
	private int fvm ;
	private int fva ;
	private int cs ;
	private int notificacion ;
	private int privacidad;
	
	public int getPrivacidad() {
		return privacidad;
	}
	public void setPrivacidad(int privacidad) {
		this.privacidad = privacidad;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public String getNtt() {
		return ntt;
	}
	public void setNtt(String ntt) {
		this.ntt = ntt;
	}
	public String getTt() {
		return tt;
	}
	public void setTt(String tt) {
		this.tt = tt;
	}
	public String getNt() {
		return nt;
	}
	public void setNt(String nt) {
		this.nt = nt;
	}
	public int getFvm() {
		return fvm;
	}
	public void setFvm(int fvm) {
		this.fvm = fvm;
	}
	public int getFva() {
		return fva;
	}
	public void setFva(int fva) {
		this.fva = fva;
	}
	public int getCs() {
		return cs;
	}
	public void setCs(int cs) {
		this.cs = cs;
	}
	public int getNotificacion() {
		return notificacion;
	}
	public void setNotificacion(int notificacion) {
		this.notificacion = notificacion;
	}
	
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		
		ActionErrors errors = new ActionErrors() ;
		
		HttpSession session = request.getSession( ) ;
		String idioma = "";
		
		ntt = validaciones.htmlentities(ntt) ;
		ntt = validaciones.quitaLinks(ntt);
		
		tt = validaciones.htmlentities(tt) ;
		tt = validaciones.quitaLinks(tt);
		
		nt = validaciones.htmlentities(nt) ;
		nt = validaciones.quitaLinks(nt);
		
		if (session.getAttribute("idioma") != null && session.getAttribute("idioma") != ""){
			idioma = session.getAttribute("idioma").toString();
	
		}
		mapping.findForward("success"+idioma);
		
		if( privacidad == 0  ) {
			
			errors.add("privacidadVacio", new ActionMessage("com.redpack.redpost.web.errors.modules.registro.privacidad"+idioma));
			request.setAttribute("ok", "0") ;
			request.setAttribute("privacidad", ""+privacidad) ;
//			return errors ;
		}
		
		else if( ntt.equals( null ) || ntt.trim( ).length( ) == 0 ){
			
			errors.add("NombreVacio", new ActionMessage("com.redpack.redpost.web.errors.modules.edicion.nombre"));
			request.setAttribute("ok", "0") ;
			
		}else
			if( tt.equals( "no" ) ){
				
				errors.add("Tipo", new ActionMessage("com.redpack.redpost.web.errors.modules.edicion.tipo"));
				request.setAttribute("ok", "0") ;
				
			}else
				if( nt.equals( null ) || nt.trim( ).length( ) == 0 ){
					
					errors.add("Numero", new ActionMessage("com.redpack.redpost.web.errors.modules.edicion.numero"));
					request.setAttribute("ok", "0") ;
					
				}else
					if( fvm == 0 || fva == 0 ){
						
						errors.add("Fecha", new ActionMessage("com.redpack.redpost.web.errors.modules.edicion.fecha"));
						request.setAttribute("ok", "0") ;
						
					}else
						if( cs == 0 ){
							
							errors.add("Code", new ActionMessage("com.redpack.redpost.web.errors.modules.edicion.codigo"));
							request.setAttribute("ok", "0") ;
							
						}
		
		if( errors.size( ) > 0 ){
			
			try{
//				HttpSession session = request.getSession( ) ;
				UserLogin login = (UserLogin) session.getAttribute("UserLogin") ;
				
		    	UserTo user = UserDAO.getCompleteUser( login.getUser( ) ) ;
		    	
		    	ArrayList datos = UserDAO.getUserRedpost( user.getId( ) ) ;
		    	
		    	ArrayList redpostL = codiceCode.decodificaDatos( Integer.parseInt( datos.get( 0 ).toString( ) ), datos.get( 1 ).toString(), datos.get( 2 ).toString( ),
		    			datos.get( 3 ).toString( ), datos.get( 4 ).toString( ), datos.get( 5 ).toString( ), datos.get( 6 ).toString( ) ) ;
		    	
		    	String status = redpostL.get( 0 ).toString( ).substring( 12 ) ;
		    	
		    	redpostL.add(5, datos.get( 7 ) ) ;
		    	redpostL.add(6, datos.get( 8 ).toString( ) ) ;
		    	
		    	request.setAttribute( "st", status ) ;
		    	request.setAttribute( "redpost", redpostL ) ;
		    	
		    }catch( Exception e ){
		    	
		    	e.printStackTrace( ) ;
		    	
		    }
			
		}
		
		request.setAttribute("ntt", ""+ntt ) ;
		request.setAttribute("tt", ""+tt) ;
		request.setAttribute("nt", ""+nt) ;
		request.setAttribute("fvm", ""+fvm) ;
		request.setAttribute("fva", ""+fva ) ;
		request.setAttribute("cs", ""+cs) ;
		request.setAttribute("fvmSel", ""+fvm);
		request.setAttribute("fvaSel", ""+fva);
		
		request.setAttribute("submenu", "mi_tarjeta");
		
		return errors ;
		
	}

}
